package com.yanqu.road.dao.impl.channelcenter.washe;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.washe.data.WasheHonorMoment;
import com.yanqu.road.entity.activity.washe.data.WasheHonorMomentTemp;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WasheHonorMomentDaoImpl extends TempDataObjectDao<WasheHonorMoment> {

    public static String getWasheHonorMomentSql = "select * from `t_u_washe_honor_moment` where `userId`=?";

    public WasheHonorMoment getWasheHonorMoment(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getWasheHonorMomentSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateWasheHonorMomentSql = "update `t_u_washe_honor_moment` set honorMomentTempMap=?,lastGameMomentTempList=? where userId=?";

    @Override
    protected boolean updateData(WasheHonorMoment washeHonorMoment) {
        washeHonorMoment.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeHonorMoment.getHonorMomentTempMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(new ArrayList<>(washeHonorMoment.getLastGameMomentTempList()))));
        para.put(i++, new DbParameter(Types.BIGINT, washeHonorMoment.getUserId()));
        if (getDb().executeNoneQuery(updateWasheHonorMomentSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertWasheHonorMomentSql = "insert into `t_u_washe_honor_moment`(userId,honorMomentTempMap,lastGameMomentTempList) values (?,?,?)";

    @Override
    protected boolean addData(WasheHonorMoment washeHonorMoment) {
        washeHonorMoment.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, washeHonorMoment.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeHonorMoment.getHonorMomentTempMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(new ArrayList<>(washeHonorMoment.getLastGameMomentTempList()))));
        if (getDb().executeNoneQuery(insertWasheHonorMomentSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected WasheHonorMoment parseData(ResultSet rs) throws SQLException {
        WasheHonorMoment washeHonorMoment = new WasheHonorMoment();
        washeHonorMoment.setUserId(rs.getLong("userId"));
        washeHonorMoment.setHonorMomentTempMap(JSON.parseObject(rs.getString("honorMomentTempMap"), new TypeReference<ConcurrentHashMap<Integer, WasheHonorMomentTemp>>() {
        }));
        washeHonorMoment.setLastGameMomentTempList(JSON.parseObject(rs.getString("lastGameMomentTempList"), new TypeReference<List<WasheHonorMomentTemp>>() {
        }));
        return washeHonorMoment;
    }


}
